TOOL.Category = "Shield Systems"
TOOL.Name = "#Shield Dome"
TOOL.Command = nil
TOOL.ConfigName = ""
TOOL.Tab = "Contraption Engine"

TOOL.ClientConVar["material"] = "models/props_combine/com_shield001a"
TOOL.ClientConVar["impactmat"] = "models/shiny"
TOOL.ClientConVar["HP"] = "1000"
TOOL.ClientConVar["Radius"] = "200"
TOOL.ClientConVar["R"] = 255
TOOL.ClientConVar["G"] = 255
TOOL.ClientConVar["B"] = 255
TOOL.ClientConVar["AutoWeld"] = "0"
TOOL.ClientConVar["model"] = "models//hunter//misc//sphere2x2.mdl"
TOOL.ClientConVar["origin"] = "above"
TOOL.EntityClass = "Shield_Bubble_Generator"

if (SERVER) then
	CreateConVar('sbox_maxShieldDomes', 4)
end

if ( CLIENT ) then
	language.Add( "Tool_shield_dome_name", "Shield Dome Generator" )
	language.Add( "Tool_shield_dome_desc", "Projects a spherical shield dome that protects against weapons fire" )
	language.Add( "Tool_shield_dome_0", "Left-Click to Spawn a Shield, Right-Click to Set Model" )
	
	language.Add( "Undone_shield_dome", "Undone shield dome" )
	
	language.Add( "Cleanup_shield_dome", "Cleanup Shield Domes" )
	language.Add( "Cleaned_shield_dome", "Cleaned up all Shield Domes" )
	language.Add( "SBoxLimit_shield_dome", "Reached the Shield Limit" )
end

function TOOL:SpawnEntity(entity, position, trace)
	local newentity = ents.Create(entity)
	newentity:SetAngles( trace.HitNormal:Angle() + Angle(90,0,0))
	newentity:SetModel( self:GetClientInfo("model") )
	newentity:SetPos(position - Vector(0, 0, newentity:OBBMins().z))
	newentity.MaxHP = math.Clamp(self:GetClientNumber("HP"), 500, 40000)
	newentity.Radius = self:GetClientNumber("Radius")
	newentity.DomeMaterial = self:GetClientInfo("material")
	newentity.ImpactMaterial = self:GetClientInfo("impactmat")
	newentity.ImpactColor = Vector(self:GetClientNumber("R"), self:GetClientNumber("G"), self:GetClientNumber("B"))
	newentity.Formdirection = self:GetClientInfo("origin")
	newentity:SetPlayer(self:GetOwner())
	newentity:Spawn()
	
	if util.tobool(self:GetClientNumber("AutoWeld")) then
		local phys = newentity:GetPhysicsObject()  	
		if (phys:IsValid()) then  		
			local weld = constraint.Weld(newentity, trace.Entity, 0, trace.PhysicsBone, 0)
			local nocollide = constraint.NoCollide(newentity, trace.Entity, 0, trace.PhysicsBone)
		end 
	end
	
	self:GetOwner():AddCount( "ShieldDomes", newentity ) 
	
	undo.Create("Shield_Dome")
		undo.AddEntity( newentity )
		undo.AddEntity( weld )
		undo.AddEntity( nocollide )
		undo.SetPlayer( self:GetOwner() )
	undo.Finish()
end

function TOOL:LeftClick( trace )
if (CLIENT) then return true end
if ( !trace.Hit ) then return end
local ply = self:GetOwner()
if (trace.Entity:GetClass() == "shield_bubble_generator") then
	local shieldmod = trace.Entity
	local HP = self:GetClientNumber("HP")
	local radius = self:GetClientNumber("Radius")
	shieldmod.MaxHP = math.Clamp(HP, 500, 40000)
	shieldmod.Radius = radius
	shieldmod.RegenAmount = HP / math.log10(HP) / 40
	shieldmod.EnergyDemand = radius * HP / 200000
	shieldmod.DomeMaterial = self:GetClientInfo("material")
	shieldmod.ImpactMaterial = self:GetClientInfo("impactmat")
	shieldmod.ImpactColor = Vector(self:GetClientNumber("R"), self:GetClientNumber("G"), self:GetClientNumber("B"))
	shieldmod.Formdirection = self:GetClientInfo("origin")
	if (shieldmod.Dome and shieldmod.Dome:IsValid()) then shieldmod.Dome:Remove() end
	return true
else
if (!ply:CheckLimit( "ShieldDomes" )) then return end
	
	if ( SERVER && !util.IsValidPhysicsObject( trace.Entity, trace.PhysicsBone ) ) then return false end
	
	local SpawnPos = trace.HitPos
	self:SpawnEntity( "shield_bubble_generator", SpawnPos, trace)
	return true
end
end

function TOOL:RightClick( trace )
	if CLIENT and trace.Entity:IsValid() then return true end
	if not trace.Entity:IsValid() then return end
	local model = trace.Entity:GetModel()
	self:GetOwner():ConCommand("shield_dome_model ".. model)
end

if CLIENT then
list.Add("DomeMaterials", "models/props_combine/stasisshield_sheet")
list.Add("DomeMaterials", "models/props_combine/com_shield001a")
list.Add("DomeMaterials", "models/shadertest/shader4")

list.Add("ImpactMaterials", "models/shiny")
list.Add("ImpactMaterials", "models/alyx/emptool_glow")
list.Add("ImpactMaterials", "models/props_c17/fisheyelens")
list.Add("ImpactMaterials", "models/shadertest/shader3")

local options = {}
options["Form From Above"] = {shield_dome_origin = "above"}
options["Form From Below"] = {shield_dome_origin = "below"}

function TOOL.BuildCPanel( CPanel )
	CPanel:ClearControls()
	CPanel:AddHeader()
	CPanel:AddDefaultControls()
	// HEADER
	CPanel:AddControl( "Header", { Text = "#Tool_shield_dome_name", Description = "#Tool_shield_dome_desc" }  )
	CPanel:AddControl( "CheckBox", { Label = "Autoweld", Command = "shield_dome_AutoWeld" } )
	CPanel:AddControl( "ComboBox", { Label = "Projection Origin", MenuButton = 0, Options = options})
	CPanel:AddControl( "Slider", { Label = "Shield Radius", Command = "shield_dome_Radius", min = 100, max = 6000 } )
	CPanel:AddControl( "Slider", { Label = "Shield Health", Command = "shield_dome_HP", min = 500, max = 40000 } )
	CPanel:AddControl( "Color", { Label = "Impact Color", Red = "shield_dome_R", Green = "shield_dome_G", Blue = "shield_dome_B", ShowAlpha = 0, ShowHSV = 1, ShowRGB = 1, Multiplier = 255})
	CPanel:AddControl( "MatSelect", { Label = "Dome Material", Height = "3", ItemWidth = 84, ItemHeight = 84, Options = list.Get("DomeMaterials"), convar = "shield_dome_material"})
	CPanel:AddControl( "MatSelect", { Label = "Impact Effect", Height = "3", ItemWidth = 84, ItemHeight = 84, Options = list.Get("ImpactMaterials"), convar = "shield_dome_impactmat"})
end
end